const db = require('../../lib/conn_oracle')

exports.getLbValues = async (req,res)=>{
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let sql = `select * from c##wxw.lb_values
               order by cust_type  
              `
    let result = await db.search2(sql)
    resMsg = result.errorNum ? `获取失败` : `获取成功`
    resStatus = result.errorNum ? 0 : 1
    resDatas = { result }
    res.json({
        msg:resMsg,
        status: resStatus,
        resDatas
    })
}

exports.editLbValue = async (req,res)=>{
    // console.log(req.body)
    let { type,form } = req.body
    let { V_NUM,VALUE,DESCRIPTION,CUST_TYPE } = form
    let reqData = []
    let msgType = ""
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let sql = ""
    if(type === 'add' || type === 'copy'){
        msgType = type === 'add' ? '添加' : '复制'
        sql = `insert into c##wxw.lb_values (V_NUM,VALUE,DESCRIPTION,CUST_TYPE)
               values ('',:VALUE,:DESCRIPTION,:CUST_TYPE)
              `
        reqData = [VALUE,DESCRIPTION,CUST_TYPE]
    }
    if(type === 'update'){
        msgType = '更新'
        sql = `update c##wxw.lb_values
               set VALUE = :VALUE,
                   DESCRIPTION = :DESCRIPTION,
                   CUST_TYPE = :CUST_TYPE
               where V_NUM = :V_NUM
              `
        reqData = [VALUE,DESCRIPTION,CUST_TYPE,V_NUM]
    }
    if(type === 'delete'){
        msgType = '删除'
        sql = `delete from c##wxw.lb_values
               where V_NUM = :V_NUM
              `
        reqData = [V_NUM]
    }

    try{
        let result = await db.writeTab(sql,reqData)
        resMsg = result.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = result.errorNum ? 0 : 1
        resDatas = { result }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}

exports.editManyLbValue = async (req,res)=>{
    // console.log(req.body)
    let { type,form } = req.body
    let options = {
        autoCommit: true
    }
    let msgType = ""
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let sql = ""
    if(type === 'batchAdd'){
        msgType = '新增'
        sql = `insert into c##wxw.lb_values (V_NUM,VALUE,DESCRIPTION,CUST_TYPE)
               values (:V_NUM,:VALUE,:DESCRIPTION,:CUST_TYPE)
              `
    }
    if(type === 'batchUpdate'){
        msgType = '更新'
        sql = `update c##wxw.lb_values
               set VALUE = :VALUE,
                   DESCRIPTION = :DESCRIPTION,
                   CUST_TYPE = :CUST_TYPE
               where V_NUM = :V_NUM
              `
    }
    if(type === 'batchDel'){
        msgType = '删除'
        sql = `delete from c##wxw.lb_values
               where V_NUM = :V_NUM
              `
    }
    try{
        let result = await db.writeManyTab(sql,form,options)
        resMsg = result.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = result.errorNum ? 0 : 1
        resDatas = { result }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}
